Page({
  data: {
    passport: {}
  },
  onLoad({
    id,
    encryptedData
  }) {
    if (id) this.getPassportFn(id)
    if (encryptedData) this.getPassportByEncryptedData(encryptedData)

  },
  // 有id调用这个接口
  async getPassportFn(id) {
    const {
      code,
      data: passport
    } = await wx.http.get('/visitor/' + id)
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      passport
    })
  },
  // 分享出去的调用这个接口
  async getPassportByEncryptedData(encryptedData) {
    const {
      code,
      data: passport
    } = await wx.http.get('/visitor/share/' + encryptedData)
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      passport
    })
  },
  onShareAppMessage() {
    return {
      title: '走过路过不要错过',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + this.data.passport.encryptedData,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
  async saveQRCode() {
    // 将网络图片地址转换成临时本地图片地址
    const {
      path
    } = await wx.getImageInfo({
      src: this.data.passport.url,
    })
    // 将本地图片存到相册里
    wx.saveImageToPhotosAlbum({
      filePath: path,
    })
  }
})